﻿/********************************************************************
	文件：PlaneShape.cs
    作者：Pangdudu
    日期：2021/03/02
    描述: 双面面板的形状
*********************************************************************/

using UnityEngine;

namespace ShapeMesh
{
    public class PlaneShape : Shape
    {
        private static readonly string _planeMeshName = "Plane Mesh";//mesh的名称

        #region private members
        private readonly float _xSize, _zSize;
        #endregion

        #region ctor
        public PlaneShape(float xSize, float zSize, MeshPivot meshPivot = MeshPivot.Center) : base(meshPivot, _planeMeshName)
        {
            _xSize = xSize;
            _zSize = zSize;

            _vertexOffset = GetVertexOffset();
        }
        #endregion

        #region override functions
        /// <summary>
        /// 根据mesh中心点的位置，获得顶点位置的偏移量
        /// </summary>
        /// <returns></returns>
        protected sealed override Vector3 GetVertexOffset()
        {
            return _meshPivot switch
            {
                MeshPivot.Center => Vector3.zero,
                MeshPivot.Top => Vector3.zero,
                MeshPivot.Bottom => Vector3.zero,
                MeshPivot.Left => new Vector3(-_xSize, 0, 0) * 0.5f,
                MeshPivot.Right => new Vector3(_xSize, 0, 0) * 0.5f,
                MeshPivot.Front => new Vector3(0, 0, _zSize) * 0.5f,
                MeshPivot.Back => new Vector3(0, 0, -_zSize) * 0.5f,
                _ => Vector3.zero
            };
        }

        /// <summary>
        /// 获得顶点的数据集合
        /// </summary>
        /// <returns></returns>
        protected override Vector3[] GetVertices()
        {
            var p0 = new Vector3(-_xSize, 0, -_zSize) * 0.5f - _vertexOffset;//左下
            var p1 = new Vector3(-_xSize, 0, _zSize) * 0.5f - _vertexOffset;//左上
            var p2 = new Vector3(_xSize, 0, _zSize) * 0.5f - _vertexOffset;//右上
            var p3 = new Vector3(_xSize, 0, -_zSize) * 0.5f - _vertexOffset;//右下

            return new []
            {
                p0,p1,p2,p3,
                p3,p2,p1,p0
            };
        }

        /// <summary>
        /// 获得法线方向的数据集合
        /// </summary>
        /// <returns></returns>
        protected override Vector3[] GetNormals()
        {
            return new []
            {
                Vector3.up,Vector3.up,Vector3.up,Vector3.up,
                Vector3.down,Vector3.down,Vector3.down,Vector3.down
            };
        }

        /// <summary>
        ///  获得三角面顶点的索引
        /// </summary>
        /// <returns></returns>
        protected override int[] GetTriangles()
        {
            return new []
            {
                0,1,2,2,3,0,
                4,5,6,6,7,4
            };
        }

        /// <summary>
        /// 获得UV坐标的数据集合
        /// </summary>
        /// <returns></returns>
        protected override Vector2[] GetUVs()
        {
            return new []
            {
                new Vector2(0,0),new Vector2(0,1),new Vector2(1,1),new Vector2(1,0),
                new Vector2(0,0),new Vector2(0,1),new Vector2(1,1),new Vector2(1,0),
            };
        }
        #endregion
    }
}